package com.ruoyi.base.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.base.mapper.BaseWarehouseMapper;
import com.ruoyi.base.domain.BaseWarehouse;
import com.ruoyi.base.service.IBaseWarehouseService;

import javax.annotation.PostConstruct;

/**
 * 仓库Service业务层处理
 * 
 * @author yepanpan
 * @date 2024-12-20
 */
@Service
public class BaseWarehouseServiceImpl implements IBaseWarehouseService 
{
    public static final String CACHE_MAP = "base_warehouse";
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private BaseWarehouseMapper baseWarehouseMapper;

    @PostConstruct
    public int reCache(){
        redisCache.deleteObject(CACHE_MAP);
        return 0;
    }

    /**
     * 查询仓库
     *
     * @param name 仓库名
     * @return 仓库
     */
    @Override
    public BaseWarehouse selectBaseWarehouseByName(String name){
        String key = "name_"+name;
        Long houseId = redisCache.getCacheMapValue(CACHE_MAP, name);
        if(houseId != null){
            return selectBaseWarehouseById(houseId);
        }

        BaseWarehouse baseWarehouse = baseWarehouseMapper.selectBaseWarehouseByName(name);
        if(baseWarehouse != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseWarehouse.getId());
            redisCache.setCacheMapValue(CACHE_MAP, "id_"+baseWarehouse.getId(), baseWarehouse);
        }
        return baseWarehouse;
    }

    /**
     * 查询仓库
     * 
     * @param id 仓库主键
     * @return 仓库
     */
    @Override
    public BaseWarehouse selectBaseWarehouseById(Long id)
    {
        String key = "id_"+id;
        BaseWarehouse baseWarehouse = redisCache.getCacheMapValue(CACHE_MAP, key);
        if(baseWarehouse != null){
            return baseWarehouse;
        }

        baseWarehouse = baseWarehouseMapper.selectBaseWarehouseById(id);
        if(baseWarehouse != null){
            redisCache.setCacheMapValue(CACHE_MAP, key, baseWarehouse);
            redisCache.setCacheMapValue(CACHE_MAP, "name_"+baseWarehouse.getName(), baseWarehouse.getId());
        }
        return baseWarehouse;
    }

    /**
     * 查询仓库列表
     * 
     * @param baseWarehouse 仓库
     * @return 仓库
     */
    @Override
    public List<BaseWarehouse> selectBaseWarehouseList(BaseWarehouse baseWarehouse)
    {
        return baseWarehouseMapper.selectBaseWarehouseList(baseWarehouse);
    }

    /**
     * 新增仓库
     * 
     * @param baseWarehouse 仓库
     * @return 结果
     */
    @Override
    public int insertBaseWarehouse(BaseWarehouse baseWarehouse)
    {
        BaseWarehouse old = baseWarehouseMapper.selectBaseWarehouseByName(baseWarehouse.getName());
        if(old != null){
            throw new ServiceException(baseWarehouse.getName() + " 仓库已存在");
        }
        baseWarehouse.setCreateTime(DateUtils.getNowDate());
        return baseWarehouseMapper.insertBaseWarehouse(baseWarehouse);
    }

    /**
     * 修改仓库
     * 
     * @param baseWarehouse 仓库
     * @return 结果
     */
    @Override
    public int updateBaseWarehouse(BaseWarehouse baseWarehouse)
    {
        BaseWarehouse old = baseWarehouseMapper.selectBaseWarehouseByName(baseWarehouse.getName());
        if(old != null && !old.getId().equals(baseWarehouse.getId())){
            throw new ServiceException(baseWarehouse.getName() + " 仓库已存在");
        }
        baseWarehouse.setUpdateTime(DateUtils.getNowDate());
        return baseWarehouseMapper.updateBaseWarehouse(baseWarehouse) + reCache();
    }

    /**
     * 批量删除仓库
     * 
     * @param ids 需要删除的仓库主键
     * @return 结果
     */
    @Override
    public int deleteBaseWarehouseByIds(Long[] ids)
    {
        return baseWarehouseMapper.deleteBaseWarehouseByIds(ids) + reCache();
    }

    /**
     * 删除仓库信息
     * 
     * @param id 仓库主键
     * @return 结果
     */
    @Override
    public int deleteBaseWarehouseById(Long id)
    {
        return baseWarehouseMapper.deleteBaseWarehouseById(id) + reCache();
    }
}
